package com.fs.jzzn.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fs.base.pojo.Attachment;
import com.fs.base.repository.AttachmentRepository;
import com.fs.base.utils.ApplicationUtils;
import com.fs.base.utils.ExcelUtils;
import com.fs.base.vo.JsonResponse;
import com.fs.base.vo.SysPath;
import com.fs.jzzn.pojo.FixedAssets;
import com.fs.jzzn.repository.FixedAssetsRepository;

/**
 * @Title: jzznpm
 * @Desc: 固定资产Controller
 * @Author: ACE
 * @Date: 2018-11-22-0022 22:12
 * @Version: 1.0
 */
@Log4j
@Controller
@RequestMapping("/console/fixedAssets/rewrite")
public class FixedAssetsController {
    @Autowired
    private FixedAssetsRepository fixedAssetsRepository;
    @Autowired
    private AttachmentRepository attachmentRepository;
    @Getter @Setter @Value("${layui.upload.path}")
    private String uploadPath;

    @RequestMapping("toImport")
    public String toImport(){
        return SysPath.BACK_PATH.concat("/fixedAssets").concat("/import");
    }

    @ResponseBody
    @RequestMapping("import")
    public JsonResponse imports(HttpServletRequest request){
        try {
        	int count = 0;
            String attachmentId = request.getParameter("attachmentId");
            if(StringUtils.isNotBlank(attachmentId)){
            	Attachment attachment = attachmentRepository.findOne(attachmentId);
				List<List<String>> data = ExcelUtils.readData(attachment.getUrl());
				if(data==null || data.size()==0)  return  JsonResponse.error("导入模板格式有误，请选择正确模板！");
				
				Map<Integer, String> titleMap = new HashMap<Integer, String>();
				List<String> titleList = data.get(0);//表头数据
				int len = titleList.size(); //表格栏目长度
				for(int i=0;i<len;i++){
					titleMap.put(i, titleList.get(i));
				}
				//删除标题行
				if(data.size()>0){ data.remove(0); }
				
				List<String> row = new ArrayList<String>();
				for(int j=0;j<len;j++){
					
				}
            }
            return JsonResponse.success("成功导入："+ count + "条数据！");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(e.getMessage());
        }
    }

    //导入Excel文件
    private List<FixedAssets> importXls(List<List<String>> readTxt,String attId) throws InvocationTargetException, IllegalAccessException {
        List<String> str = readTxt.get(0);
        String[] names = new String[str.size()];
        String[] name = getName(str.toArray(names));
        List<FixedAssets> list = new ArrayList<>();
        for (int i = 1; i < readTxt.size(); i++) {
            List<String> s = readTxt.get(i);
            Map<String,String> map = new HashMap<>();
            int count = 0;
            if(s.size() < name.length){
                for (int j = s.size()-1; j < name.length; j++) {
                    s.add("");
                }
                count = s.size();
            }else {
                count = name.length;
            }
            for (int i1 = 0; i1 < count; i1++) {
                map.put(name[i1],s.get(i1));
            }
            FixedAssets fixed = new FixedAssets();
            BeanUtils.populate(fixed,map);
            fixed.setCreaterId(ApplicationUtils.userId());
            list.add(fixed);
        }
        return list;
    }

    //根据第一行数据附件对应的数据对象
    private String[] getName(String[] str){
        List<String> name = new ArrayList<>();
        Map<String,String> map = FixedAssets.map;
        for (String s : str) {
            if(map.containsKey(s)){
                name.add(map.get(s));
            }
        }
        String[] names = new String[name.size()];
        return name.toArray(names);
    }
}
